﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Helpers
{
    public static class StringEx
    {

        public static bool IsInt(this string s)
        {
            int result;
            return int.TryParse(s, out result);
        }
        public static bool IsDateTime(this string s)
        {
            DateTime result;
            if (DateTime.TryParse(s, out result))
            {
                return (result - DateTime.MinValue).Days > 1;
            }
            else
            {
                return false;
            }
        }
        public static bool IsNullOrWhiteSpace(this string s)
        {
            return s == null ? true : string.IsNullOrWhiteSpace(s.Trim());
        }
        public static bool IsNotNullOrEmpty(this string s)
        {
            return !string.IsNullOrEmpty(s);
        }
        ///// <summary>
        ///// 类型转换，支持int(0),duble(0),decimal(0),bool(false),datetime([0001/1/1 0:00:00]),可空值类型
        ///// </summary>
        ///// <typeparam name="T"></typeparam>
        ///// <param name="s"></param>
        ///// <returns></returns>
        //public static T ToOtherType<T>(this string s)
        //{
        //    string fullName = typeof(T).FullName;
        //    try
        //    {
        //        if (fullName.Contains("Int16"))
        //            return Convert.ToInt16(s);
        //        if (fullName.Contains("Int32"))
        //            return Convert.ToInt32(s);
        //        if (fullName.Contains("Int64"))
        //            return Convert.ToInt64(s);
        //        if (fullName.Contains("Double"))
        //            return Convert.ToDouble(s);
        //        if (fullName.Contains("Decimal"))
        //            return Convert.ToDecimal(s);
        //        if (fullName.Contains("Boolean"))
        //            return Convert.ToBoolean(s);
        //        if (fullName.Contains("DateTime"))
        //            return Convert.ToDateTime(s);
        //        return default(T);
        //    }
        //    catch (Exception)
        //    {
        //        return default(T);
        //    }
        //}

        public static string Replace(this string s, string[] array, string p)
        {
            for (int i = 0; i < array.Length; i++)
            {
                s = s.Replace(array[i], p);
            }
            return s;
        }
        public static int ToInt(this string s)
        {
            int result;
            if (int.TryParse(s, out result))
                return result;
            else
                return default(int);
        }
    }
}
